<template>
  <li>
    <label>
      <input type="checkbox" :checked="todoObj.isDone" @change="changeStatus(todoObj.id)"/>
      <span>{{ todoObj.name }}</span>
      <input type="text" :value="todoObj.name" v-show="todoObj.isEdit" ref="input" @blur="changeName">
    </label>
    <button class="btn btn-danger" @click="delTodo(todoObj.id)">删除</button>
    <button class="btn btn-edit" @click="editTodo(todoObj)">编辑</button>
  </li>
</template>

<script>
export default {
  name: "MyItem",
  props: ["todoObj"],
  methods: {

    changeStatus(id) {
      this.$bus.$emit("changeStatus", id)
    },

    changeName(e) {
      this.todoObj.isEdit = false;
      this.$bus.$emit("changeName",this.todoObj.id,e.target.value)
    },
    editTodo(todoObj) {
      if (todoObj.hasOwnProperty("isEdit")) {
        todoObj.isEdit = true;
      } else {
        this.$set(todoObj, "isEdit", true)
      }
      this.$nextTick(
          () => {
            this.$refs.input.focus();
          }
      )

    },
    delTodo(id) {
      this.$bus.$emit("delTodo", id)
    }
  }
}
</script>

<style scoped lang="less">


li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li:hover {
  button {
    display: block;
  }
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

</style>
